package pun.potato;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
/**
 * AwesomeAdapter is a Custom class to implement custom row in ListView
 * 
 * @author Adil Soomro
 *
 */
@SuppressLint("ResourceAsColor")
public class BubbleAdapter extends BaseAdapter{
	private Context mContext;
	private ArrayList<Pun> mMessages;

	public BubbleAdapter(Context context, ArrayList<Pun> messages) {
		super();
		this.mContext = context;
		this.mMessages = messages;
	}

	@Override
	public int getCount() {
		return mMessages.size();
	}
	@Override
	public Object getItem(int position) {		
		return mMessages.get(position);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Pun message = (Pun) this.getItem(position);

		ViewHolder holder; 
		if(convertView == null)
		{
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row, parent, false);
			holder.message = (TextView) convertView.findViewById(R.id.message_text);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			
			
			convertView.setTag(holder);
		}
		else
			holder = (ViewHolder) convertView.getTag();

		holder.message.setText(message.getMessage());
		holder.pun_id = message.getPunId();
		
		LayoutParams lpMess = (LayoutParams) holder.message.getLayoutParams();
		LayoutParams lpIcon = (LayoutParams) holder.icon.getLayoutParams();
		
		//check if it is a status message then remove background, and change text color.
		//if(message.isStatusMessage())
		//{
		//	holder.message.setBackgroundDrawable(null);
		//	lp.gravity = Gravity.LEFT;
		//	holder.message.setTextColor(R.color.textFieldColor);
		//}
		//else
		//{		
			//Check whether message is mine to show green background and align to right
			if(message.isMine())
			{
				holder.message.setBackgroundResource(R.drawable.light_left);
				lpIcon.gravity = Gravity.LEFT;
				lpMess.gravity = Gravity.LEFT;
				lpMess.setMargins((int)(PlayScreen.x*.1), 2, (int)(PlayScreen.x*.3), 0);
			
				if(message.upvoted){
				holder.icon.setImageResource(R.drawable.nice_panda_gold);
				holder.icon.setTag(R.drawable.nice_panda_gold);
				
				} else {
					holder.icon.setImageResource(R.drawable.nice_panda);
					holder.icon.setTag(R.drawable.nice_panda);
				}
				
				ImageView myImg= holder.icon;
				//myImg.setOnClickListener(new View.OnClickListener() {
				//            public void onClick(View view) {
				//            	Log.d("Panda upvoting","Panda clicked");
				                //Launch Intent or whatever you want here
				            	//give the partner a mojo point
				            	
				//            }
				//        });
			}
			//If not mine then it is from sender to show orange background and align to left
			else
			{
				holder.message.setBackgroundResource(R.drawable.dark_right);

				if(message.upvoted){
					//Log.d("evil panda", "gold");
					holder.icon.setImageResource(R.drawable.evil_panda_gold);
					holder.icon.setTag(R.drawable.evil_panda_gold);
				} else {	
					//Log.d("evil panda", ""+message.getUpvoted());
					holder.icon.setImageResource(R.drawable.evil_panda);
					holder.icon.setTag(R.drawable.evil_panda);
				}
				
				lpIcon.gravity = Gravity.RIGHT;
				lpMess.gravity = Gravity.RIGHT;
				lpMess.setMargins((int)(PlayScreen.x*.3), 1, (int)(PlayScreen.x*.1), 0);
			}
			holder.message.setLayoutParams(lpMess);
			lpIcon.width = 100;
			lpIcon.height = 80;
			holder.icon.setLayoutParams(lpIcon);
			//holder.message.setTextColor(R.color.textColor);	
		//}
		return convertView;
	}
	public static class ViewHolder
	{
		TextView message;
		ImageView icon;
		int pun_id;
		int upvoted = 0;
	}

	@Override
	public long getItemId(int position) {
		//Unimplemented, because we aren't using Sqlite.
		return 0;
	}

}